package creational_pattern.protopyte_pattern;

import java.io.*;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/12/02 14:49
 */


public class DeepCopyProtype {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        WeekLog weekLog = new WeekLog();
        Attachment attachment = new Attachment();
        attachment.setName("Mike");
        weekLog.setAttachment(attachment);
        weekLog.setContent("content");
        weekLog.setName("Amy");
        WeekLog clone = weekLog.deepClone();
        System.out.print("weekLog是否与克隆对象相同：");
        System.out.println(clone.equals(weekLog));
    }
}

class Attachment implements Serializable {
    private String name;

    public void download() {
        System.out.println("下载附件,name:" + name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class WeekLog implements Serializable {
    private Attachment attachment;
    private String name;
    private String content;

    //深拷贝
    public WeekLog deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        WeekLog o = (WeekLog) ois.readObject();
        return o;


    }

    public Attachment getAttachment() {
        return attachment;
    }

    public void setAttachment(Attachment attachment) {
        this.attachment = attachment;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}